<style lang="less">
    @import "../../carDealer.less";
    .s1{
        .ivu-select-dropdown-list {
            height:200px
        }
    }

</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card >
                    <Row>
                        <h2 align="center">普通车商信息</h2>

                        <br>
                        <br>
                        <Form ref="carDealerForm" :model="carDealerForm" :label-width="130"  inline
                              v-bind:disabled="true"  >
                            <Row>

                                <Col span="8">
                                    <FormItem label="合作商名称" prop="channelId" style="width: 350px" v-if="this.typeCode =='1'"  >
                                        <Select v-model="carDealerForm.channelId"  placeholder="请选择" clearable filterable style="width: 200px" class="s1" >
                                            <Option v-for="(item,index) in channelData" :value="item.id" :label="item.channelFullName"  :key="index" ></Option>
                                        </Select>
                                    </FormItem>
                                </Col>


                                <Col span="8">
                                    <FormItem label="车商" prop="cardealerName" style="width: 350px">
                                        <Input type="text" v-model.trim="carDealerForm.cardealerName" placeholder="请输入" style="width: 200px" clearable/>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem label="车商代码" prop="cardealerCode" style="width: 350px">
                                        <Input type="text" v-model.trim="carDealerForm.cardealerCode" placeholder="系统自动生成" style="width: 200px" disabled/>
                                    </FormItem>
                                </Col>

                            </Row>


                            <Row>
                                <Col span="8">
                                    <Form-item label="合作模式" prop="cooperationMode" style="width: 350px">
                                        <radio-group v-model.trim="carDealerForm.cooperationMode">  <!--@on-change="selectRadio"-->
                                            <radio label="0">本地合作</radio>
                                            <radio label="1">异地调车</radio>
                                        </radio-group>
                                    </Form-item>
                                </Col>

                                <Col span="8">
                                    <Form-item label="车商性质" prop="cardealerProperties" style="width: 350px">
                                        <Select placeholder="请选择" v-model="carDealerForm.cardealerProperties" style="width: 200px">
                                            <Option value="0">汽贸</Option>
                                            <Option value="1">4S</Option>
                                            <Option value="2">其他</Option>
                                        </Select>
                                    </Form-item>
                                </Col>

                                <Col span="8">
                                    <FormItem label="所在区域" prop="domicLocation" style="width:350px;" >
                                        <Cascader :data="location" v-model.trim="carDealerForm.domicLocation" filterable style="width:200px;"
                                                  :load-data="loadData"></Cascader>
                                    </FormItem>
                                </Col>
                            </Row>

                            <Row>
                                <Col span="8">
                                    <Form-item label="法人代表" prop="corporateName" style="width: 350px">
                                        <Input type="text" v-model.trim="carDealerForm.corporateName" clearable placeholder="请输入" style="width: 200px"/>
                                    </Form-item>
                                </Col>

                                <Col span="8">
                                    <FormItem label="手机号" prop="telephoneNumber" style="width: 350px">
                                        <Input type="text" placeholder="请输入" v-model.trim="carDealerForm.telephoneNumber"  style="width: 200px"/>
                                    </FormItem>
                                </Col>

                                <Col span="8">
                                    <Form-item label="统一社会信用代码" prop="socUniCrtCode" style="width: 350px">
                                        <Input type="text" clearable placeholder="请输入" v-model.trim="carDealerForm.socUniCrtCode"
                                               style="width: 200px"   />
                                    </Form-item>
                                </Col>
                            </Row>
                            <Row>

                                <Col span="8">
                                    <Form-item label="注册资金(单位：万元)" prop="registeredCapital"  >
                                        <Input type="text" clearable style="width: 200px" v-model.trim="carDealerForm.registeredCapital"/>
                                    </Form-item>
                                </Col>

                                <Col span="8">
                                    <Form-item label="业务员" prop="salesMan" style="width: 350px" >
                                        <Select placeholder="请选择" v-model="carDealerForm.salesMan" filterable  class="s1"
                                                clearable style="width: 200px">
                                            <Option v-for="(item,index) in salesManData" :value="item.username" :label="item.userRealName" :key="index" >
                                            </Option>                  
                                        </Select>
                                    </Form-item>
                                </Col>


                                <Col span="8">
                                    <Form-item label="注册时间" prop="registrationTime" style="width:350px">

                                        <Date-picker type="date" placeholder="选择日期" v-model="carDealerForm.registrationTime"
                                                     style="width:200px"></Date-picker>

                                    </Form-item>
                                </Col>


                            </Row>

                            <Row>
                                <Col span="8">
                                    <FormItem label="注册省市" prop="addressinfo" >
                                        <Cascader :data="location" v-model="carDealerForm.addressinfo" style="width:200px;"
                                                  :load-data="loadData"></Cascader>
                                    </FormItem>
                                </Col>


                                <Col span="8">
                                    <Form-item label="详细地址" prop="address" >
                                        <Input type="text" v-model.trim="carDealerForm.address" placeholder="请输入..."
                                               style="width: 200px"></Input>
                                    </Form-item>
                                </Col>

                                <Col span="8">
                                    <Form-item label="地址经纬度" prop="longitudeLatitude" style="width: 350px">
                                        <Input type="text" clearable v-model.trim="carDealerForm.longitudeLatitude" style="width: 200px" placeholder="输入详细地址后带出"/>
                                    </Form-item>
                                </Col>



                            </Row>
                            <Row>

                                <Col span="8">
                                    <Form-item label="备注" prop="receivingRemarks" >
                                        <Input type="textarea" :rows="4" v-model.trim="carDealerForm.receivingRemarks"
                                               placeholder="请输入..." style=" width: 200px"></Input>
                                    </Form-item>
                                </Col>

                                <Col span="8">
                                    <FormItem  prop="inDepotInfo"  label="是否在库" >
                                        <radio-group v-model="inDepotInfo">
                                            <radio label="1" disabled>是</radio>
                                            <radio label="0" disabled>否</radio>
                                        </radio-group>
                                    </FormItem>
                                </Col>

                                <Col span="8">
                                    <FormItem  prop="mainBrand"  label="主要经营品牌">
                                        <Tree :data="baseData" style="height:200px;overflow-y:auto;display: inline-block;" ref="baseTree" show-checkbox v-model="carDealerForm.mainBrand"   multiple> </Tree>
                                    </FormItem>
                                </Col>


                            </Row>
                        </Form>

                    </Row>

                </Card>
            </Col>
        </Row>



    </div>


</template>


<script>
    import {
        saveCarDealerData,
        editCarDealerData,
        saveBlack,
        blackCleack,
        getCarDealerInfoById,
        getAllSPChannels,
        saveCarDealerDataToCase,
        getAllSaleManByChannelId,
        getCommonCarDealerInDepotInfo,
    } from "@/projects/afs-channel/api/car-dealer/dealer";
    import circleLoading from "_c/circle-loading/circle-loading.vue";
    import PlaceCascade from "@/components/place-cascade";
    import {getLocation} from "@/projects/afs-channel/api/common/common";
    // import {province, city, area} from "_p/afs-apply/assets/js/staticData.js";
    import {
        getAllAddressList,
        getAllCarList,
    } from "_p/afs-channel/api/common/common.js"
    import {getCarBrandById,getChannelBasicInfoById,} from  "@/projects/afs-channel/api/dealer/dealer";
    import Form from "../../../../../../components/iview";
    import MapModal from '_p/afs-channel/pages/common/map/MapModal';


    export default {
        name: "carDealer-check",
        components: {
            Form,
            circleLoading
        },
        data() {
            return {


                mapModal: false, // 地图弹窗
                mapLocation:'', // 地图定位
                mapAddress:'', // 详细地址
                canOpenMapModal: false, // 是否可以打开地图弹窗

                aaa:"",
                channelFullName: "",
                //主营品牌的变量
                expandLevel: 0,
                carLevel: -1,
                liveNode:false,
                //是否业务人员关联车商 1：是；0:否； 默认否
                personRelCardealerSwitch:"0",
                /**
                 * 渠道id 现在写死 后期改***************
                 * **/
                blackApplyVisible: false,
                modalTitle: "",
                notInBlackList: true,
                //判断是否是合作商非管理人员 后面要改
                isEmployee: false,

                locationData: [],
                location: [],
                isShowFile: false,
                loading: true,
                typeCode: "",
                status: "00",
                //合作商
                channelData: [],
                channelForm:{
                    channelId:"",
                    channelFullName:"",
                },
                oldChannelId:"",
                showSalesMan:false,
                //合作商下所有业务员集合
                salesManData:[],
                inDepotInfo:"",
                carDealerForm: {

                    //渠道id
                    channelId:"",
                    //更新前渠道id
                    //所在区域
                    domicLocation:[],
                    addressinfo: [
                    ],

                    locationAddressinfo: [
                        {
                            detailAddress: []
                        }
                    ],
                    associAccount: "",
                    id: "",
                    channelFullName: "",
                    cardealerName: "",
                    cardealerCode: "",
                    cooperationMode: "0",
                    cardealerProperties: "",
                    location: "",
                    corporateName: "",
                    telephoneNumber: "",
                    socUniCrtCode: "",
                    longitudeLatitude: "",
                    longitudeLatitudeRange: "",
                    salesMan: "",
                    registrationTime: "",
                    mainBrand: [],
                    address: "",
                    receivingRemarks: "",
                    status: "01",
                    cardealerType: "",

                    channelProvince: "",
                    channelCity: "",
                    channelAddrValue: [],
                    locationAddr:"",
                    //注册资金
                    registeredCapital:"",
                },

                channelAddrrLabel: "地址",
                channelAddrValue: "channelAddrValue",

                //黑名单
                blackApplyForm: {},
                //黑名单
                blackApplyFormValidate: {},

                baseData2: [{
                    expand: true,
                    title: '全部资料(0)',
                    children: [{
                        title: '车商备案影像件(0)',
                        children: [{
                            title: '其他(0)'
                        }]
                    }]
                }],
                baseData: [],
                userModalVisible: false,
                fileFrom: {
                    productName: ""
                },
                submitLoading: false,
                data: [],
            };
        },

        components: {
            PlaceCascade,
        },

        methods: {
            init() {

            },

            // 有关省市的
            getLocationData() {
                let param = ""
                param = {
                    level: '1',
                    upperCode: '1'
                };
                getLocation(param).then((res) => {
                    if (res.code == "0000") {
                        res.data.forEach(item => {
                            // 表明是否是父节点
                            if (item.isParent) {
                                getLocation({upperCode: item.value}).then(res => {
                                    if (res.code === "0000") {
                                        item.children = res.data;
                                        this.location.push(item);
                                    }
                                })
                            }
                        })
                    }
                    // this.location.push(...res.data);
                })
            },


            //调用省市的方法
            loadData(item, callback) {
                item.loading = true;
                getLocation({upperCode: item.value}).then(res => {
                    if (res.code === "0000") {
                        res.data.forEach(function (item) {
                            if (item.isParent) {
                                item.loading = false;
                                item.children = [];
                            }
                        });
                        item.children = res.data;
                        item.loading = false;
                        callback();
                    }
                })
            },


            //根据车商id查询基本信息
            getCarDealerInfo(v) {
                getCarDealerInfoById(v).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        this.carDealerForm = res.data;
                        this.carDealerForm.registeredCapital = res.data.registeredCapital + "";
                        this.carDealerForm.id = v;
                        // 地址信息
                        this.carDealerForm.domicLocation = [
                            this.carDealerForm.locationProvince,
                            this.carDealerForm.locationCity
                        ];
                        this.carDealerForm.addressinfo = [
                            this.carDealerForm.channelProvince,
                            this.carDealerForm.channelCity
                        ];
                        this.carDealerForm.channelId = this.afs.getPageParams(this).channelId + "";

                        getCommonCarDealerInDepotInfo(res.data.socUniCrtCode).then(res => {
                            if(res.data != "0"){
                                this.inDepotInfo = "1";
                            }else{
                                this.inDepotInfo ="0";
                            }
                        })

                        /*getChannelBasicInfoById(this.carDealerForm.channelId).then(res => {
                            if (res.code === "0000") {
                                this.personRelCardealerSwitch = res.data[0].personRelCardealerSwitch;
                            }
                        });*/

                    }
                })
            },

            //主营品牌
            getBaseTreeList(v) {
                let param = {
                    type: [],
                }
                getAllCarList(param).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        // 仅展开指定级数 默认一级
                        let expandLevel = this.carLevel;
                        res.data.forEach(function (e) {
                            if (expandLevel == "-1") {
                                if (e.carLevel === "-1") {
                                    e.expand = true;
                                }
                                if (e.children && e.children.length > 0) {
                                    e.children.forEach(function (c) {
                                        if (c.carLevel === "0") {
                                            c.expand = false;
                                        }
                                    });
                                }
                            } else {
                                if (e.children && e.children.length > 0) {
                                    e.children.forEach(function (c) {
                                        if (expandLevel == 0) {
                                            if (c.carLevel === "0") {
                                                c.expand = false;
                                            }
                                        }
                                    });
                                }
                            }
                        });
                        this.baseData = res.data;
                        if (v != null && v != "" && v != undefined) {
                            //根据渠道id查询主营品牌
                            getCarBrandById(v).then(resMainCar => {
                                this.loading = false;
                                if (resMainCar.code === "0000") {
                                    let cars = resMainCar.data;
                                    this.checkMainCarTree(this.baseData, cars);
                                }
                            })
                        }
                    }
                });
            },

            // 递归判断子节点
            checkMainCarTree(mainCarData, cars) {
                let that = this;
                mainCarData.forEach(function (p) {
                    if (that.hasMainCar(p, cars)) {
                        if (p.carLevel == -1 || p.carLevel == 0) {
                            //层级表示第一层级，第二层级不用勾选
                            p.checked = false;
                        } else {
                            p.checked = true;
                        }
                    } else {
                        p.checked = false;
                    }
                    if (p.children && p.children.length > 0) {
                        that.checkMainCarTree(p.children, cars);
                    }
                });
            },

            //判断主营品牌节点勾选
            hasMainCar(p, cars) {
                let flag = false;
                for (let i = 0; i < cars.length; i++) {
                    if (p.id === cars[i].code) {
                        flag = true;
                        break;
                    }
                }
                if (flag) {
                    return true;
                }
                return false;
            },

            //主营品牌
            selectBaseTree() {
                let permIds = [];
                let selectedNodes = this.$refs.baseTree.getCheckedAndIndeterminateNodes();
                selectedNodes.forEach(function (e) {
                    permIds.push(e.id + "," + e.title);
                });

                this.carDealerForm.mainBrand = permIds;
            },

            getAllSaleManByChannelId(v) {
                //获取当前合作商下面的所有业务员
                let businessType = "01";
                getAllSaleManByChannelId(v, businessType).then(res => {
                    if (res.code === "0000") {
                        this.salesManData = res.data;
                    }
                });
            },
            checkLongitudeLatitudeRange(){
                if(!this.longitudeLatitudeRange){
                    this.$parent.inputRange();
                }
            },

        },
        mounted() {
            this.init();
        }
    };
</script>
